package asteroids.engine;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import asteroids.entity.Ship;

public class CompactHUD extends HUD
{
    private static final Logger logger = Logger.getLogger(CompactHUD.class);

    private ArrayList<Ship> ships;

    @Override
    public void initialize(GameState state)
    {
        ships = new ArrayList<Ship>();
        ships.addAll(state.getShipManager().getObjects());
    }

    @Override
    public void draw(Graphics g)
    {
        int x = Utils.GAME_WIDTH - 100;
        int y = 10;
        for( Ship s : ships )
        {
            drawCompactInfo(g, s, x, y);
            y += 40;
        }
    }

    private void drawCompactInfo(Graphics g, Ship s, int x, int y)
    {
        g.setColor(Color.GRAY);
        g.drawRect(x, y, 90, 30);
        Utils.CartesianCoordinate[] proxyShape = s.getCollisionBounds();
        for( int i = 0; i < proxyShape.length; i++ )
        {
            proxyShape[i] = proxyShape[i].shiftBy(x + 15 - s.getXPosition(),
                    y + 15 - s.getYPosition());
        }
        g.setColor(Color.LIGHT_GRAY);
        Utils.drawCartesianPolygon(proxyShape, g);
        g.setColor(Color.GREEN);
        drawMeter((int) s.getIntegrity(), x + 30, y + 5, g);
        g.setColor(Color.RED);
        drawMeter((int) s.getEnergy(), x + 30, y + 16, g);
    }

    private void drawMeter(int value, int x, int y, Graphics g)
    {
        logger.trace("drawMeter, value = " + value);
        int iters = 0;
        int currentx = x;
        int currenty = y;
        while (iters < value && iters < 40)
        {
            iters++;
            // draw another pip
            g.fillRect(currentx, currenty, 2, 4);
            currentx = (currenty > y) ? currentx + 3 : currentx;
            currenty = (currenty > y) ? y : y + 5;
        }
    }
}
